package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //添加菜品表
        this.save(dishDto);

        //获取菜品id
        Long dishId = dishDto.getId();

        //获取口味对象集合
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //添加口味数据
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据菜品id查询数据
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //创建disDto对象
        DishDto dishDto = new DishDto();
        //根据id查询菜品对象
        Dish dish = this.getById(id);
        //数据拷贝
        BeanUtils.copyProperties(dish,dishDto);
        //创建查询构造器
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件根据菜品id查询口味表
        lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        //查询口味表
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        //给disDto对象的flavors属性赋值
        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 更新菜品信息和菜品口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //修改菜品表
        this.updateById(dishDto);

        //修改菜品口味表,先删除当前菜品口味数据重新添加
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        //删除当前菜品口味表数据
        dishFlavorService.remove(lambdaQueryWrapper);

        //添加服务器修改的当前菜品口味数据
        List<DishFlavor> flavorList = dishDto.getFlavors();

        flavorList = flavorList.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavorList);
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithFlavor(List<Long> ids) {
        //获取到删除菜品的id
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();

        setmealDishLambdaQueryWrapper.in(ids!=null,SetmealDish::getDishId,ids);
        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
        //判断该菜品是否关联套餐
        if (setmealDishList.size() != 0 && ids.size() == 1){
            //如果关联套餐则不能删除
            throw new CustomException("当前菜品在套餐中不能删除");
        }else if (setmealDishList.size() != 0 && ids.size() > 1){
            throw new CustomException("当前有菜品在套餐中不能删除");
        }

        //如果不关联套餐则直接删除在把关联的口味删除
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(ids!=null,DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);

        this.removeByIds(ids);
    }

    /**
     * 菜品停售和起售
     *
     * @param ids
     * @param status
     */
    @Override
    @Transactional
    public void updateWithSetmeal(List<Long> ids, Integer status) {

        //判断一下当前菜品中是否有关联套餐
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(ids!=null,SetmealDish::getDishId,ids);

        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);

        if (setmealDishList.size() == 0){
            //如果没有关联套餐则直接停售或起售
            LambdaUpdateWrapper<Dish> dishLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            dishLambdaUpdateWrapper.in(ids!=null,Dish::getId,ids);
            dishLambdaUpdateWrapper.set(status!=null,Dish::getStatus,status);

            this.update(dishLambdaUpdateWrapper);
        }else {

        }

        //如果有关联套餐,则关联的套餐一起停售或起售


    }
}
